<?php
/**
 * 企业资质申请控制器
 */

namespace app\api\controller;

use app\api\service\ApplyService;
use app\common\model\Apply;
use app\common\validate\ApplyValidate;
use hg\apidoc\annotation as Apidoc;
use think\response\Json;

/**
 * @Apidoc\Title("公司申请")
 * @Apidoc\Group("index")
 */
class ApplyController extends ApiBaseController {
	protected array $loginExcept = [
		'api/apply/add',
	];
	/**
	 * @Apidoc\Title("列表")
	 * @Apidoc\Desc("列表")
	 * @Apidoc\Method("GET")
	 * @Apidoc\Url("/api/apply/index")
	 * @Apidoc\Returned("data",        type="int",                 desc="desc")
	 */
	public function index(ApplyService $service): Json {
		try {
			$this->param['user_id'] = $this->uid;
			$data = $service->getList($this->param, $this->page, $this->limit);
			$result = [
				'apply' => $data,
			];

			return api_success($result);
		} catch (ApiServiceException $e) {
			return api_error($e->getMessage());
		}
	}
	/**
	 * @Apidoc\Title("公司申请")
	 * @Apidoc\Desc("公司申请")
	 * @Apidoc\Method("POST")
	 * @Apidoc\Url("/api/apply/add")
	 * @Apidoc\Param("company_name", type="string", desc="公司名称"),
	 * @Apidoc\Param("license", type="string", desc="营业执照照片"),
	 * @Apidoc\Param("id_card_top", type="string", desc="身份证正面"),
	 * @Apidoc\Param("id_card_back", type="string", desc="身份证反面"),
	 * @Apidoc\Param("cate_id", type="string", desc="所属类目"),
	 * @Apidoc\Param("nature", type="string", desc="公司性质"),
	 * @Apidoc\Param("aptitude", type="string", desc="纳税人资质"),
	 * @Apidoc\Param("profession", type="string", desc="所属行业"),
	 * @Apidoc\Param("name", type="string", desc="联系人姓名"),
	 * @Apidoc\Param("phone", type="string", desc="手机号"),
	 * @Apidoc\Param("email", type="string", desc="邮箱"),
	 * @Apidoc\Param("address", type="string", desc="地址"),
	 * @Apidoc\Param("client_num", type="string", desc="客户数"),
	 * @Apidoc\Param("car_num", type="string", desc="可控车辆资源"),
	 * @Apidoc\Param("is_car", type="string", desc="是否具备运输系统 0否  1是"),
	 * @Apidoc\Param("dot_num", type="string", desc="全国网点数量"),
	 * @Apidoc\Returned("data",        type="array",                 desc="data")
	 */
	public function add(ApplyValidate $validate, ApplyService $service): Json {
		$this->param['status'] = 0;
		// $check = $validate->scene('api_add')->check($this->param);
		// if (!$check) {
		// 	return api_error($validate->getError());
		// }
		$this->param['cate'] = explode(',', $this->param['cate']);
// 		$find = Apply::where('company_name', $this->param['company_name'])->where('status', 0)->findOrEmpty();
// 		if (!$find->isEmpty()) {
// 			return api_error('等待管理员审核后再提交');
// 		}
		return api_error('已暂停提交');
		$dishone = $this->dishone($this->param['company_name']);

		if (isset($dishone['code']) && $dishone['code'] == 0) {
			$this->param['dishone'] = $dishone['result']['items'];
			$this->param['dishone_num'] = count($this->param['dishone']);
		} else {
			$this->param['dishone'] = array();
			$this->param['dishone_num'] = 0;
		}
		$result = $service->createData($this->param);

		return $result ? api_success() : api_error();
	}
	public function dishone($name) {

		$url = "https://dishone.market.alicloudapi.com/lundear/dishone?keyword=" . urlencode($name);
		$appcode = "ed173fcc03a64ae28c3ab2c69b53009c";
		$headers = array();
		array_push($headers, "Authorization:APPCODE " . $appcode);
		$result = $this->get_data($url, $headers);
		return $result;

	}
	public function get_data($url, $headers) {
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
		curl_setopt($curl, CURLOPT_FAILONERROR, false);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

		if (1 == strpos("$" . $url, "https://")) {
			curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
			curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
		}
		$output = curl_exec($curl);
		curl_close($curl);
		return json_decode($output, true);
	}

}
